package com.example.college;



import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;



import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditLecturesForDoctors extends Activity {

	// Progress Dialog
		private ProgressDialog pDialog;
	JSONParser jsonParser = new JSONParser();
	
	EditText inputtime;
	EditText inputdate;
	
	// url to edit time and date
	private static String url_edit_date_time = "http://192.168.1.12:8080/WebApplication2/webresources/doctor/";
	
	
	// JSON Node names
		private static final String TAG_SUCCESS = "success";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_lectures_for_doctors);
		
		//Edit Text
		inputtime=(EditText) findViewById(R.id.editTime);
		inputdate=(EditText) findViewById(R.id.editdate);
		
		//Create Button
		Button btnedit=(Button) findViewById(R.id.submit);
		
		//Button Click event
		btnedit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				//edit time and date of lecture in background thread
				new EditLecture().execute();
				
				
			}
		});
	
	}
	
	
	
	class EditLecture extends AsyncTask<String, String, String>{
		
		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(EditLecturesForDoctors.this);
			pDialog.setMessage("Creating Product..");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		
		/**
		 * Editing Time and Date
		 * */
		@Override
		protected String doInBackground(String... args) {
			String time=inputtime.getText().toString();
			String date=inputdate.getText().toString();
			
			// Building Parameters
			  List<NameValuePair> params = new ArrayList<NameValuePair>();
			  params.add(new BasicNameValuePair("time", time));
			  params.add(new BasicNameValuePair("date", date));
			  
			// getting JSON Object
				// Note that create product url accepts POST method
				JSONObject json = jsonParser.makeHttpRequest(url_edit_date_time,
						"POST", params);
			  
				
				// check log cat for response
				Log.d("Create Response", json.toString());

				// check for success tag
				try {
					int success = json.getInt(TAG_SUCCESS);

					if (success == 1) {
						// successfully editing
						Toast toast=Toast.makeText(getApplicationContext(), "editing success",Toast.LENGTH_LONG);
						toast.show();
						
						// closing this screen
						finish();
					} else {
						// failed to editing
						Toast toast=Toast.makeText(getApplicationContext(), "editing failed",Toast.LENGTH_LONG);
						toast.show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}

				
			return null;
		}
		
		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog once done
			pDialog.dismiss();
		}	
		
		
		
		
	}
	
	


}
